package com.wandian.texas.command.texas;

import com.wandian.texas.BaseCommandObject;
import com.wandian.texas.UserMgr;
import com.wandian.command.Cmd;
import com.wandian.command.UserCommand;
import com.wandian.texas.protocol.Protocol;

@Cmd(code = Protocol.CHATROOM_SEND_MESSAGE, desc = "")
public class ChatRoomCmd extends UserCommand {

	@Override
	public void execute(BaseCommandObject cmdObject) {
		super.execute(cmdObject);
		// 0success,1,2,3
//		Log.info(String.format("ChatRoomCmd body data部分数据 postion %s limit %s remain %s, msg:%s", cmdObject.position(), cmdObject.limit(), cmdObject.remaining(), cmdObject.byteToHex()));
		
		/*byte type = */cmdObject.readByte();// cmdObject.dataPacket.byteBuffer.get();
		String message = cmdObject.readString();// cmdObject.dataPacket.readString();
//		ChatrecordEntity chat = new ChatrecordEntity();
		
//		UserInfoEntity user = cmdObject.getUser();
//		int userid = user.getUserId();
//		chat.setUserId(userid);
//		chat.setMessage(message);
//		chat.setOp((short) 1);
//		DaoManager.ChatrecordDao(cmdObject.dbIndex).addChatrecord(chat);
		// 如果成功
		cmdObject.writeBegin();
//		cmdObject.writeString(user.getRealName().trim());
		cmdObject.writeByte((byte)0);
		cmdObject.writeInt(cmdObject.getUser().userId);
		cmdObject.writeString(cmdObject.getUser().nickName);
		cmdObject.writeString(message);
		cmdObject.writeEnd();
		UserMgr.I().broadCastToRoomUsers(cmdObject);
//		UserMgr.I().broadCastToRoomUsers(cmdObject);
//		UserMgr.I().broadCastToAllOnlines(cmdObject.getCmd(), cmdObject.getBytes());
		/*long delay = (long) Math.ceil(Math.random() * 5) * 1000;//5秒随机
		TimerCommand timer = new TimerCommand("DelayMessageCmd", Protocol.CHATROOM_DELAY_MESSAGE, delay);
		timer.put("SocketCmd", cmdObject);
		timer.put("delay", delay);
		timer.put("message", message);
		ThreadPool.getInstance().Push(timer);*/
		//		ThreadPool.getInstance().Push(new TimerCommand("DelayMessageCmd1", Protocol.CHATROOM_DELAY_MESSAGE, delay, 5));
		// new Timer().schedule(new DelaySendTask(cmdObject, message,delay),
		// delay);
		// int delay = RandomUtil.next(1, 5);
		// TimerMgr.push(new DelaySendCommand(cmdObject, message, delay),
		// delay);
		// TimerCommand command = new TimerCommand();
		// command.totalCount = 2;
		// command.delay = delay * 1000;
		// command.name = "command_chatroom";
		// command.command = new DelaySendCommand(cmdObject, message, delay);
		// TimerPool.Add(command);
		// PoolThread.
	}

	class DelaySendCommand implements Runnable {
		BaseCommandObject cmdObject;
		String message;
		int delay;

		public DelaySendCommand(BaseCommandObject _cmdObject, String _message, int _delay) {
			cmdObject = _cmdObject;
			message = _message;
			delay = _delay;
		}

		public void run() {
			cmdObject.setCmd((short) 8000);
			cmdObject.writeBegin();
			cmdObject.writeString("系统:");
			cmdObject.writeByte((byte) 0);
			cmdObject.writeString("这是服务器延时返回的信息,延时" + delay + "000毫秒,内容:" + message);
			cmdObject.writeEnd();
			UserMgr.I().SendData(cmdObject.sessionObject,cmdObject);
		}

	}
}
